package cn.pugle.base.thread;

/**
 * see {@link InterruptTest} {@link ShutdownTest} {@link ThreadKillTest}
 *
 * @author tzp
 * @since 2020/10/26
 */
public class InterruptTest {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable(), "t1");
        t1.start();

        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(t1.getState());//TIMED_WAITING
        t1.interrupt();
        System.out.println(t1.isInterrupted());
    }

    static class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " 线程创建");
            try {
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
                //抛出这个异常的地方, 会clear调interrupt的标志, 所以下面使用false
            }
            //运行时的线程被中断后，只会修改中断标记，不会抛出异常. 抛出这个异常是sleep这个方法决定这样处理interrupt
            System.out.println(Thread.currentThread().isInterrupted());//始终false
            System.out.println(Thread.currentThread().getName() + " 线程到这了");
        }

    }
}
